<?php

namespace app\api\controller;

use think\Request;
use app\api\model\Login_M ;
use \think\Validate;
use app\api\library\im\TimRestApi;



class Login
{
    protected $status;            //创建房间返回状态
    protected $data;            //创建房间返回数据
    protected $model;

    public function __construct(){
        $this->model = new Login_M;
    }
    /**
     * 操作错误返回的快捷方法
     * @access protected
     * @param string $message 错误信息
     * @param integer $status 错误状态值
     * @return json[]
     * @author 陈润璇
     */
    protected function Retterror($msgCode = '0|服务器繁忙|2000')
    {
        $messageCode = explode('|',$msgCode);
        $this->status = [
            'code'        =>  $messageCode['0'],
            'message'     =>  $messageCode['1'],
            'create_time' =>  time(),
            'err_code'    =>  $messageCode['2']
        ];
        unset($messageCode);
    }

    /**
     * 操作成功跳返回的快捷方法
     * @access protected
     * @param string $message 成功信息
     * @return json[]
     * @author 陈润璇
     */
    protected function Rettsuccess($msgCode = '0000|操作成功|0000')
    {   
       $messageCode = explode("|", $msgCode);
       $this->status  = [
           'code'         =>  $messageCode['0'],
           'message'      =>  $messageCode['1'],
           'create_time'  =>  time(),
           'err_code'     =>  $messageCode['2']
       ];
       unset($messageCode);
    }

    
    /**
     * 输出返回数据
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $type 返回类型 JSON XML
     * @param integer $code HTTP状态码
     * @return Response
     * @author 陈润璇
     */
    protected function retResponse()
    {
        $data['status']  = $this->status;
        $data['data']    = $this->data;

        return json($data);
    }
   /**
     * post api/Login/phoneLogin -- PC接口
     * API密码登录
     * @access public
     */
    public function Login()
    {   
        // if ($this->request->isPost()) {
            $date['pwd'] = trim(input('pass'));
            $date['name'] = trim(input('name'));
            $validate = new Validate([
                'name'  => 'alphaNum',
                'pass'  => 'require'
            ]);
            $data = [
                'name'  => $date['name'],
                'pass'  => $date['pwd']
            ];
            if (!$validate->check($data)) {
                $this->Retterror(config('Notice.AllError'));
                return $this->retResponse();
            }
            
            $this->uid = $date['name'];

            $ret = $this->send_msg2($date['name']);
        
            if(!$ret){
                
                $this->Retterror(config('Notice.sigError'));
                return $this->retResponse();
            }
            $date['user_sig'] = $ret[0];
            $rest = $this->model->login($date);
           
            if($rest['code'] == 0){
                $this->Retterror(config('Notice.classNot'));
            }elseif($rest['code'] == 1){
                $this->Retterror(config('Notice.NameError'));
            }elseif($rest['code'] == 2){
                $this->Retterror(config('Notice.studentError'));
            }elseif($rest['code'] == 3){
                $this->data = $rest['info'];
                $this->Rettsuccess();
            }elseif($rest['code'] == 4){
                $this->Retterror(config('Notice.ClassTeacherNotError'));
            }else{
                $this->Retterror();
            }

            return $this->retResponse();
            
        // }
    }


     public function send_msg2($user)
    {
        $root=str_replace('\\','/',realpath(dirname(__FILE__).'/../'));
        $sdkappid = '1400034416';
        $identifier = $user;
        $private_pem_path = $root.'/library/im/PEM/private_key';
        $user_sig = "";
        
        $api = new TimRestAPI();
        $api->init($sdkappid, $identifier);
        
        if($private_pem_path != "")
        {
        //独立模式
        if(!file_exists($private_pem_path))
        {
            echo "私钥文件不存在, 请确保TimRestApiConfig.json配置字段private_pem_path正确\n";
            return;
        }
        /**
        * 获取usersig
        * 36000为usersig的保活期
        * signature为获取私钥脚本，详情请见 账号登录集成 http://avc.qcloud.com/wiki2.0/im/
        */
            if(is_64bit()){
                if(PATH_SEPARATOR==':'){
                    $signature = $root."/library/im/signature/linux-signature64";
                }else{
                    $signature = $root."\\signature\\windows-signature64.exe";
                }

            }else{
                if(PATH_SEPARATOR==':')
                {
                $signature = $root."/library/im/signature/linux-signature32";
                }else{
                $signature = $root."/library/im/signature/windows-signature32.exe";
    //                dump($signature);
                }
        }
        $ret = $api->generate_user_sig($identifier, '36000', $private_pem_path, $signature);
    //        dump($ret);
        if($ret == null || strstr($ret[0], "failed")){
            echo "获取usrsig失败, 请确保TimRestApiConfig.json配置信息正确\n";
            return -1;
        }
        }else if($user_sig != ""){
        //托管模式
        $ret = $api->set_user_sig($user_sig);
        if($ret == false){
            echo "设置usrsig失败, 请确保TimRestApiConfig.json配置信息正确\n";
            return -1;
        }
        }else{
        echo "请填写TimRestApiConfig.json中private_pem_path(独立模式)或者user_sig(托管模式)字段\n";
        return -1;
        }
        // $api->set_user_sig($ret[0]);
        
        return $ret;
    }
  



    /**
    * facebook互联登录
    * @access public
    */
    public function facebookLogin(){

    }

    /**
    * twitter互联登录
    * @access public
    */
    public function twitterLogin(){

    }



}
